package com.hibox.controller;


import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hibox.common.core.domain.AjaxResult;
import com.hibox.common.utils.StringUtils;
import com.hibox.domain.Goods;
import com.hibox.service.GoodsService;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

import com.hibox.util.QiniuUtils;
import org.springframework.web.bind.annotation.*;

/**
 * 商品信息(Goods)表控制层
 *
 * @author WangFK
 * @since 2023-08-17 16:30:24
 */
@RestController
@RequestMapping("/store/goods")
public class GoodsController {
    /**
     * 服务对象
     */
    @Resource
    private GoodsService goodsService;
    @Resource
    private QiniuUtils qiniuUtils;

    @GetMapping("/goodsList")
    public AjaxResult goodsList() {
        LambdaUpdateWrapper<Goods> lqw = new LambdaUpdateWrapper<Goods>().eq(Goods::getStatus, "1");
        List<Goods> list = goodsService.list(lqw);
        return AjaxResult.success(list);
    }

    /**
     * 修改商品状态
     *
     * @param id
     * @param status
     * @return
     */
    @GetMapping("/changeGoodsStatus")
    public AjaxResult changeGoodsStatus(String id, String status) {
        LambdaUpdateWrapper<Goods> lqw = new LambdaUpdateWrapper<Goods>()
                .eq(Goods::getId, id)
                .set(Goods::getStatus, status);
        return goodsService.update(lqw) ? AjaxResult.success() : AjaxResult.error();
    }

    @GetMapping("/list")
    public AjaxResult list(Long pageNum, Long pageSize, String search, String status) {
        Page<Goods> goodsPage = new Page<>(pageNum, pageSize);
        LambdaUpdateWrapper<Goods> lqw = new LambdaUpdateWrapper<Goods>()
                .eq(ObjectUtil.isNotEmpty(status), Goods::getStatus, status);
        if (ObjectUtil.isNotEmpty(search)) {
            lqw.like(Goods::getName, search).or()
                    .like(Goods::getDescription, search).or()
                    .like(Goods::getSellingPrice, search);
        }
        goodsPage = goodsService.page(goodsPage, lqw);
        List<Goods> records = goodsPage.getRecords();
        if (ObjectUtil.isEmpty(records)) {
            return AjaxResult.success();
        }
        return AjaxResult.success()
                .put("rows", records)
                .put("total", goodsPage.getTotal());

    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public AjaxResult selectOne(@PathVariable Serializable id) {
        return AjaxResult.success(goodsService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param goods 实体对象
     * @return 新增结果
     */
    @PostMapping
    public AjaxResult insert(@RequestBody Goods goods) {
        String name = goods.getName();
        if (ObjectUtil.isEmpty(name)) {
            return AjaxResult.error("商品名称为空格");
        }
        if (ObjectUtil.isEmpty(goods.getSellingPrice())) {
            return AjaxResult.error("商品售价为空");
        }
        if (ObjectUtil.isNotEmpty(goods.getId())) {
            return update(goods);
        }
        if (!getGoodsInfoByName(name)) {
            return AjaxResult.error("商品：" + goods.getName() + "重复");
        }
        return goodsService.save(goods) ? AjaxResult.success() : AjaxResult.error();
    }

    /**
     * 修改数据
     *
     * @param goods 实体对象
     * @return 修改结果
     */
    @PutMapping
    public AjaxResult update(@RequestBody Goods goods) {
        Goods byId = goodsService.getById(goods.getId());
        String name = byId.getName();
        if (!goods.getName().equalsIgnoreCase(name)) {
            if (!getGoodsInfoByName(name)) {
                return AjaxResult.error("商品：" + goods.getName() + "重复");
            }
        }
        String img = byId.getImg();
        if (StringUtils.isNotEmpty(img)) {
            if (!img.equalsIgnoreCase(goods.getImg())) {
                qiniuUtils.deleteFileFromQiniu(img.substring(img.lastIndexOf("/")).split("/")[1]);
            }
        }
        return goodsService.updateById(goods) ? AjaxResult.success() : AjaxResult.error();
    }

    /**
     * 判断商品名是否重复
     *
     * @param name
     * @return
     */
    private boolean getGoodsInfoByName(String name) {
        LambdaUpdateWrapper<Goods> lqw = new LambdaUpdateWrapper<Goods>()
                .eq(Goods::getName, name)
                .eq(Goods::getStatus, "1");
        List<Goods> list = goodsService.list(lqw);
        if (ObjectUtil.isNotEmpty(list)) {
            return false;
        }
        return true;
    }

    /**
     * 删除数据
     *
     * @param id 主键结合
     * @return 删除结果
     */
    @DeleteMapping("{id}")
    public AjaxResult delete(@PathVariable String id) {
        List<String> split = StrUtil.split(id, ",");
        return goodsService.removeBatchByIds(split) ? AjaxResult.success() : AjaxResult.error();
    }
}

